package user

import (
	"github.com/gin-gonic/gin"
	logic2 "iot-base/auth/api/logic"
	Log "iot-base/auth/logger"
	"iot-base/auth/svc"
	types2 "iot-base/auth/types"
	"net/http"
	"strconv"
)

type LoginData struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

func UserAny(c *gin.Context) {
	method := c.Request.Method
	switch method {
	case http.MethodGet:
		listUser(c)
		return
	case http.MethodPost:
		addUser(c)
		return
	case http.MethodDelete:
		delUser(c)
		return
	case http.MethodPut:
		editUser(c)
		return
	default:
		resp := types2.NewResult(c)
		resp.ErrorCode(-1, "请求方法不存在")
	}
}

func addUser(c *gin.Context) {
	uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	userLogic := logic2.NewUserLogic()
	var user types2.ReqAddUser
	err := c.ShouldBind(&user)
	if err != nil {
		resp.ErrorCode(types2.ExceptionError, err.Error())
		return
	}
	err = userLogic.AddUser(uid, user)
	if err != nil {
		resp.ErrorCode(types2.ExceptionError, err.Error())
		return
	}
	resp.Success(gin.H{})
	return
}

func editUser(c *gin.Context) {
	resp := types2.NewResult(c)
	userLogic := logic2.NewUserLogic()
	var user types2.ReqUpdateUser
	err := c.ShouldBind(&user)
	if err != nil {
		resp.ErrorCode(types2.ExceptionError, err.Error())
		return
	}
	err = userLogic.UpdateUser(user)
	if err != nil {
		resp.ErrorCode(types2.ExceptionError, err.Error())
		return
	}
	resp.Success(gin.H{})
	return
}

func delUser(c *gin.Context) {
	resp := types2.NewResult(c)
	userLogic := logic2.NewUserLogic()
	var id types2.ReqId
	err := c.ShouldBind(&id)
	if err != nil {
		resp.ErrorCode(types2.ExceptionError, "")
		return
	}
	err = userLogic.Del(id.ID)
	// 删除用户组关联
	svc.Dao.TUserGroup.DelGroupRole(id.ID, 0)
	if err != nil {
		resp.ErrorCode(types2.ExceptionError, "")
		return
	}
	resp.Success(gin.H{})
	return
}

func listUser(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	userLogic := logic2.NewUserLogic()
	var req types2.ReqFindUser
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	result, err := userLogic.UserList(uid, req)
	if err != nil {
		Log.Log.Errorf("[%v] FindCount err:=%v", requestId, err)
		resp.ErrorCode(types2.ExceptionError, "")
		return
	}
	resp.Success(result)
	return
}

// DetailUser 用户详情.
func DetailUser(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	//uid := c.GetInt64("uid")
	resp := types2.NewResult(c)
	strId := c.Param("id")
	if strId == "" {
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	userId, _ := strconv.ParseInt(strId, 10, 64)
	userLogic := logic2.NewUserLogic()
	result, err := userLogic.DetailUser(userId)
	if err != nil {
		Log.Log.Errorf("[%v] FindCount err:=%v", requestId, err)
		resp.ErrorCode(types2.ExceptionError, "")
		return
	}
	resp.Success(result)
}

// ResetPwd 重置密码.
func ResetPwd(c *gin.Context) {
	//uid := c.GetInt64("uid")
	requestId, _ := c.Get("RequestId")
	resp := types2.NewResult(c)
	var req types2.ReqResetPwd
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	userLogic := logic2.NewUserLogic()
	err = userLogic.ResetPwd(req.Id, req.Pwd)
	if err != nil {
		Log.Log.Errorf("[%v] FindCount err:=%v", requestId, err)
		resp.ErrorCode(types2.ExceptionError, "")
		return
	}
	resp.Success(gin.H{})
}

func ChangePwd(c *gin.Context) {
	uid := c.GetInt64("uid")
	requestId, _ := c.Get("RequestId")
	resp := types2.NewResult(c)
	var req types2.ReqChangePwd
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	userLogic := logic2.NewUserLogic()
	code, err := userLogic.ChangePwd(uid, req.Pwd, req.NewPwd)
	if err != nil {
		Log.Log.Errorf("[%v] FindCount err:=%v", requestId, err)
		resp.ErrorCode(types2.ExceptionError, "")
		return
	}
	resp.ErrorCode(code, "")
}

func OperationLogs(c *gin.Context) {
	uid := c.GetInt64("uid")
	requestId, _ := c.Get("RequestId")
	resp := types2.NewResult(c)
	var req types2.ReqOperation
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types2.ExceptionError, err.Error())
		return
	}
	operation := svc.Dao.TOperationLog
	total, err := operation.FindCount(uid, req.LogType, req.StartDate, req.EndDate)
	if err != nil {
		Log.Log.Errorf("[%v] FindCount err:=%v", requestId, err)
		resp.ErrorCode(types2.ExceptionError, "")
		return
	}
	list, err := svc.Dao.TOperationLog.Find(uid, req.LogType, req.StartDate, req.EndDate, req.Page, req.Size)
	if err != nil {
		Log.Log.Errorf("[%v] FindCount err:=%v", requestId, err)
		resp.ErrorCode(types2.ExceptionError, err.Error())
		return
	}
	respData := gin.H{
		"total": total,
		"list":  list,
	}
	resp.Success(respData)
	return
}
